package de.preamp.clients.java.utils.ui;

import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * @author André Stannek
 * @since 2010-03-22
 * @version 2010-03-22
 */
public class PreampTray extends TrayIcon implements ActionListener
{
    /**
     * the mainUI belonging to this tray.
     */
    private final transient MainUI mainUI;

    /**
     * the menu items.
     */
    private final transient MenuItem exitItem, showItem;

    /**
     * @param image
     *            the tray image
     * @param mainUI
     *            the mainUI belonging to this tray
     */
    public PreampTray(final Image image, final MainUI mainUI)
    {
	super(image, "preamp player");
	this.setImageAutoSize(true);
	this.mainUI = mainUI;
	mainUI.setVisible(false);

	final PopupMenu popup = new PopupMenu();
	exitItem = new MenuItem("Quit");
	exitItem.addActionListener(this);
	showItem = new MenuItem("Show");
	showItem.addActionListener(this);
	popup.add(showItem);
	popup.add(exitItem);
	this.setPopupMenu(popup);
	this.addActionListener(this);
	this.addMouseListener(new MouseAdapter()
	{
	    @Override
	    public final void mouseClicked(final MouseEvent event)
	    {
		mainUI.setVisible(!mainUI.isVisible());
	    }
	});
    }

    @Override
    public final void actionPerformed(final ActionEvent event)
    {
	if (event.getSource() == exitItem)
	{
	    System.exit(0);
	}
	else if (event.getSource() == showItem)
	{
	    mainUI.setVisible(!mainUI.isVisible());
	}
    }
}
